<?php

namespace app\api\controller\cron;
use app\admin\model\CouponRecord;
use app\admin\model\Order;
use app\admin\model\order\Operations;
use app\api\service\OrderService;
use app\api\service\RoomService;
use app\common\controller\Api;
use app\common\model\Lights;
use app\common\model\member\order\Items;
use app\common\model\member\product\Specs;
use app\common\model\Occupants;
use app\common\model\Pauserecords;
use app\common\model\Reservations;
use app\common\model\Rooms;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Base;
use think\Db;
use think\Request;

/**
 * 计划任务 - 优惠券
 */
class Coupon extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * 优惠券到期失效
     * https://你的域名/addons/tongcheng/cron.Coupon/expire
     */
    public function expire()
    {
        $time  = time();
        $count = \app\admin\model\Coupon::where('status', 1)
            ->where('end_time', '<', $time)
            ->update(['status' => 2]);// 2:已过期

        $count += CouponRecord::where('status', 0)
            ->where('expire_time', '<', $time)
            ->update(['status' => 2]);// 2:已过期

        $this->success('请求成功', $count);
    }

    public function order_lose(){
        //预约订单结束
       $order= Reservations::where('deposit_status','=',0)->where('expire_pay_time','lt',time())->select();
        foreach ($order as $row) {
            $result = Reservations::where('id',$row['id'])->where('deposit_status','=',0)->update(['deposit_status'=>2,'cancel_time'=>time()]);
            if($result>0){
            }
        }

        //商城订单过期结束
        $memorder= \app\common\model\member\Order::where('status','=',0)->where('expire_pay_time','lt',time())->select();
        foreach ($memorder as $row) {
            \app\common\model\member\Order::where('id',$row['id'])->where('status','=',0)->update(['status'=>4,'cancel_time'=>time()]);
            //加库存
            $Items=Items::where('order_id','=',$row['id'])->select();
            foreach ($Items as $item) {
                if($item['specs_id']==null){
                    \app\common\model\member\Products::where('id',$item['product_id'])->setInc('stock',$item['num']);
                }else{
                    Specs::where('id',$item['specs_id'])->setInc('stock',$item['num']);
                }
            }
        }

        $this->success('请求成功', []);
    }




    public function yuyue()
    {


        $tenMinutesAgo = date('Y-m-d H:i:s', strtotime('-10 minutes'));

        $reservations = Reservations::where('start_time', '<', $tenMinutesAgo)
            ->where('reservation_status', '=', 0)
            ->where('deposit_status', '=', 1)
            ->select();

        foreach ($reservations as $row) {
            $reservations_id = $row->id;
            $Reservations = \app\common\model\Reservations::where('id', $reservations_id)->where('deposit_status', 1)->find();
            $room_id = $Reservations['room_id'];
            $room = Rooms::where('id', $room_id)->find();
            $shop_id = $room['shop_id'];

            $excludedLightIds = Occupants::where('status', '<>', 2)
                ->column('lights_id');

            $lights_id = Lights::whereNotIn('id', $excludedLightIds)
                ->orderRaw('rand()')
                ->limit(1)
                ->value('id');

            //查找没使用的灯码
            $result = Occupants::create([
                'user_id' => $row['user_id'],
                'room_id' => $room_id,
                'shop_id' => $shop_id,
                'join_time' => time(),
                'status' => 0,
                'lights_id' => $lights_id,
                'reservations_id' => $reservations_id,
                'unit_price' => $room['deposit']
            ]);
            \app\common\model\Reservations::where('id', $reservations_id)->update(['reservation_status' => 1, 'number_used' => $Reservations['number_used'] + 1]);
        }
        $this->success('请求成功', []);
    }

    public function yuyue_end()
    {
        $reservations = Reservations::where('end_time', '<=', date('Y-m-d H:i:s')) // 当前时间
        ->where('reservation_status', '=', 1) // 状态为“已预约”
        ->select();
        foreach ($reservations as $row) {

            $Occupants = Occupants::where('reservations_id', '=', $row['id'])->find();
            if (!$Occupants) {
                continue;
            }
            if ($Occupants['status'] != 0) {
                continue;
            }
            //如果有暂停 结束
            $Pause = Pauserecords::where('occupant_id', $Occupants['id'])->where('status', 1)->find();
            if ($Pause) {
                $time = time();
                Pauserecords::where('id', $Pause['id'])->update(['status' => 2, 'resume_time' => $time]);
                Occupants::where('id', $Occupants['id'])->update(['pause_status' => 1, 'pause_time' => $Occupants['pause_time'] + ($time - $Pause['pause_time'])]);
            }
            Occupants::where('id', $Occupants['id'])->update(['status' => 1, 'leave_time' => strtotime($row['end_time'])]);

        }
        $this->success('请求成功', []);
    }


}
